﻿using System;
using BenjaminSchroeter.Dsl.CompartmentMapping;
using Microsoft.VisualStudio.Modeling;

namespace BenjaminSchroeter.CompartmentMapping
{
    partial class Parent1ReferencesParent2BuilderInstance
    {
        protected override ElementLink CreateElementLink
            (Parent1 source, 
             SelectedCompartmentPartType sourcePartType,
             Entry1 sourceEntry, 
             Parent2 target,
             SelectedCompartmentPartType targetPartType, 
             Entry2 targetEntry)
        {
            Parent1ReferencesParent2 result = new Parent1ReferencesParent2(source, target);
            if ( sourcePartType == SelectedCompartmentPartType.Head )
                // use a empty guid for the Head
                result.fromEntry = Guid.Empty; 
            else
                result.fromEntry = sourceEntry.Guid;

            if (targetPartType == SelectedCompartmentPartType.Head)
                // use a empty guid for the Head
                result.toEntry = Guid.Empty;
            else
                result.toEntry = targetEntry.Guid;

            return result;
        }

        public override bool IsEntryConnectionSource(Entry1 entry, Parent1ReferencesParent2 connection)
        {
            if (entry == null)
                return connection.fromEntry.Equals(Guid.Empty);

            return connection.fromEntry.Equals(entry.Guid);
        }

        public override bool IsEntryConnectionTarget(Entry2 entry, Parent1ReferencesParent2 connection)
        {
            if (entry == null)
                return connection.toEntry.Equals(Guid.Empty);

            return connection.toEntry.Equals(entry.Guid);
        }

    }
}
